//
//  ChineseString.m
//  YZX_ChineseSorting
//
//  Created by Suilongkeji on 13-10-29.
//  Copyright (c) 2013年 Suilongkeji. All rights reserved.
//

#import "ChineseString.h"
#import "NSString+PinYinExtention.h"

@implementation ChineseString
@synthesize string;
@synthesize pinYin;

#pragma mark - 返回tableview右方 indexArray
+(NSMutableArray*)IndexArray:(NSArray*)stringArr
{
    NSMutableArray *tempArray = [self ReturnSortChineseArrar:stringArr];
    NSMutableArray *A_Result=[NSMutableArray array];
    NSString *tempString ;
    
    for (NSString* object in tempArray)
    {
        NSString *pinyin = [((ChineseString*)object).pinYin substringToIndex:1];
        //不同
        if(![tempString isEqualToString:pinyin])
        {
           // NSLog(@"IndexArray----->%@",pinyin);
            [A_Result addObject:pinyin];
            tempString = pinyin;
        }
    }
        return A_Result;
}

#pragma mark - 返回联系人
+(NSMutableArray*)LetterSortArray:(NSArray*)stringArr
{
    NSMutableArray *tempArray = [self ReturnSortChineseArrar:stringArr];
    NSMutableArray *LetterResult=[NSMutableArray array];
    NSMutableArray *item = [NSMutableArray array];
    NSString *tempString ;
    //拼音分组
    for (NSString* object in tempArray) {

         NSString *pinyin = [((ChineseString*)object).pinYin substringToIndex:1];
         NSString *string = ((ChineseString*)object).string;
        //不同
        if(![tempString isEqualToString:pinyin])
        {
            //分组
            item = [NSMutableArray array];
            [item  addObject:string];
            [LetterResult addObject:item];
            //遍历
            tempString = pinyin;
        }else//相同
        {
            [item  addObject:string];
        }
    }
    return LetterResult;
}


//过滤指定字符串   里面的指定字符根据自己的需要添加
+(NSString*)RemoveSpecialCharacter: (NSString *)str {
    NSRange urgentRange = [str rangeOfCharacterFromSet: [NSCharacterSet characterSetWithCharactersInString: @",.？、 ~￥#&<>《》()[]{}【】^@/￡¤|§¨「」『』￠￢￣~@#&*（）——+|《》$_€"]];
    if (urgentRange.location != NSNotFound)
    {
        return [self RemoveSpecialCharacter:[str stringByReplacingCharactersInRange:urgentRange withString:@""]];
    }
    return str;
}

///////////////////
//
//返回排序好的字符拼音
//
///////////////////
+ (NSMutableArray*)ReturnSortChineseArrar:(NSArray*)stringArr {
    //获取字符串中文字的拼音首字母并与字符串共同存放
    NSMutableArray *chineseStringsArray = [NSMutableArray array];
    for(int i = 0; i < stringArr.count; i++) {
        
        ChineseString *chineseString = [[ChineseString alloc] init];
        chineseString.string = [NSString stringWithString:[stringArr objectAtIndex:i]];
        if(chineseString.string == nil) {
            chineseString.string = @"";
        }
        //去除两端空格和回车
        chineseString.string = [chineseString.string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        
        //此方法存在一些问题 有些字符过滤不了
        //NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"@／：；（）¥「」＂、[]{}#%-*+=_\\|~＜＞$€^•'@#$%^&*()_+'\""];
        //chineseString.string = [chineseString.string stringByTrimmingCharactersInSet:set];
        
        //这里我自己写了一个递归过滤指定字符串   RemoveSpecialCharacter
        //chineseString.string =[ChineseString RemoveSpecialCharacter:chineseString.string];//fix:为什么要过滤，项目中就是需要不过滤掉得字符串
        //NSLog(@"string====%@",chineseString.string);
        
        if(chineseString.string.length == 0){
            chineseString.pinYin = @"";
        }else{
            chineseString.pinYin = [[chineseString.string pinyinForSort:YES] uppercaseString];
        }
        [chineseStringsArray addObject:chineseString];
    }
    //按照拼音首字母对这些Strings进行排序
    NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"pinYin" ascending:YES]];
    [chineseStringsArray sortUsingDescriptors:sortDescriptors];

    return chineseStringsArray;
}

#pragma mark - 返回一组字母排序数组
+(NSMutableArray*)SortArray:(NSArray*)stringArr
{
    NSMutableArray *tempArray = [self ReturnSortChineseArrar:stringArr];
    
    //把排序好的内容从ChineseString类中提取出来
    NSMutableArray *result=[NSMutableArray array];
    for(int i=0;i<[stringArr count];i++){
        [result addObject:((ChineseString*)[tempArray objectAtIndex:i]).string];
        NSLog(@"SortArray----->%@",((ChineseString*)[tempArray objectAtIndex:i]).string);
    }
    return result;
}
#pragma mark - 汉字转拼音
//获得拼音加其他字符的字符串数组
+ (NSArray *)transformChinesStringArray:(NSArray *)originStringArray{
    
    NSMutableArray *pinyinArray = [[NSMutableArray alloc]initWithCapacity:originStringArray.count];
    //找出字符串中的汉字 替换成拼音
    for (NSInteger i = 0; i < originStringArray.count; i++) {
        NSString *pinyin = [ChineseString transformChineseToPinYin:originStringArray[i]];
        [pinyinArray addObject:pinyin];
    }
    return pinyinArray;
}
//汉字转拼音 其他字符不会变
+ (NSString *)transformChineseToPinYin:(NSString *)chineseString{
    if ([chineseString isEqualToString:@""]) {
        return chineseString;
    }
    /*Boolean CFStringTransform(CFMutableStringRef string, CFRange *range, CFStringRef transform, Boolean reverse);
     其中string参数是要转换的string，比如要转换的中文，同时它是mutable的，因此也直接作为最终转换后的字符串。range是要转换的范围，同时输出转换后改变的范围，如果为NULL，视为全部转换。transform可以指定要进行什么样的转换，这里可以指定多种语言的拼写转换。reverse指定该转换是否必须是可逆向转换的。如果转换成功就返回true，否则返回false。*/
    NSMutableString *chinese = [chineseString mutableCopy];
    CFStringTransform((__bridge CFMutableStringRef)chinese, NULL, kCFStringTransformMandarinLatin, NO);
    CFStringTransform((__bridge CFMutableStringRef)chinese, NULL, kCFStringTransformStripDiacritics, NO);
    //只写一行，那就只转出带声调的拼音，只写出第二行只会返回汉字
    //可能没有对多音字进行处理
    return [chinese lowercaseString];//小写处理
}
@end
